<template>
  <div class="app-fill-page login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">Spark外卖商家后台管理系统</h3>
      <el-form-item prop="username" class="el-form-item">
        <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
          <template #prefix><svg-icon name="user" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" class="el-form-item">
        <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="() => {}">
          <template #prefix><svg-icon name="password" /></template>
        </el-input>
      </el-form-item>
      <div id="control">
        <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
        <router-link class="settle_in" active-class="active" to="/settle">商家入驻</router-link>
      </div>
      <el-form-item style="width: 100%">
        <el-button size="large" type="primary" style="width: 100%" @click.prevent="handleLogin">
          <span>登 录</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2019-2023 <a href="#" target="_blank">spark.com</a> 版权所有.</span>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance } from 'vue'
import ComponentHat from '@/plugins/type'
const { $store, $API, $modal } = <ComponentHat>getCurrentInstance().proxy
import router from '@/router'
import { LoginParam } from '@/types/param/login'
let loginForm = ref<LoginParam & { rememberMe: boolean }>({
  username: '',
  password: '',
  rememberMe: false
})
const loginRules = {
  username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }]
}
async function handleLogin() {
  const result = await $API.merchantAccount.login(loginForm.value)
  if (!result.success) {
    $modal.msgError(`登录失败！${result.message}`)
    return
  }
  $store.shop.setTokens(result.data.authenticationTokens, loginForm.value.rememberMe)
  $store.menu.setMenuList(result.data.menuList)
  $modal.msgSuccess('登录成功！')
  // 路由跳转
  location.replace('/')
}
</script>
<style lang="scss" scoped>
a {
  color: white;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('@/assets/image/login_bg.png');
  background-size: cover;
}

.title {
  margin: 0 auto 30px;
  text-align: center;
  color: #707070;
}

.login-form {
  padding: 25px 25px 5px;
  width: 450px;
  border-radius: 6px;
  background: #fff;

  .el-input {
    height: 40px;

    input {
      display: inline-block;
      height: 40px;
    }
  }

  .input-icon {
    margin-left: 0;
    width: 14px;
    height: 39px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  float: right;
  width: 33%;
  height: 40px;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 40px;
  font-size: 12px;
  text-align: center;
  color: #fff;
  line-height: 40px;
  letter-spacing: 1px;
}

.login-code-img {
  padding-left: 12px;
  height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

#control {
  display: flex;
  justify-content: space-between;
  align-items: start;
  height: 40px;

  .settle_in {
    text-decoration: none;
    color: #00e;
  }
}
</style>
